<!---
	TEMPLATE: userForm.cfm
	AUTHOR: Matt Woodward (mpwoodward@gmail.com)
	DATE: 2/20/2005
	PURPOSE: User form for preliminary CFHibernate tests
	CHANGE HISTORY:
		* 2/20/2005: template created (Matt Woodward)
		* 2/21/2005: added functionality to retrieve user if this is an edit
						(Matt Woodward)
--->

<cfparam name="url.action" type="string" default="Add" />

<cfscript>
	// get Hibernate session factory
	sessionFactory = Application.hibernateConfig.buildSessionFactory();
	// open a session
	mySession = sessionFactory.openSession();
		
	// if this is an update, use Hibernate to get the User object
	if (url.action EQ "Update") {
		// get Hibernate session factory
		//sessionFactory = Application.hibernateConfig.buildSessionFactory();
		
		// open a session
		//mySession = sessionFactory.openSession();
		
		// get the user
		hqlQueryString = "from User user where userID = " & url.id;
		user = mySession.createQuery(hqlQueryString).uniqueResult();
		
		// cleanup
		//mySession.close();
		//sessionFactory.close();
	} else {
		/* this is an add, so create an empty User object for
			purposes of setting form values */
		user = createObject("java", "User").init();
	}
</cfscript>

<html>
	<head>
		<title>CFHibernate User Form</title>
		
		<script type="text/javascript">
			function validate(f) {
				if (f.firstName.value.length == 0) {
					alert("Please enter the first name.");
					return false;
				}
				
				if (f.lastName.value.length == 0) {
					alert("Please enter the last name.");
					return false;
				}
				
				if (f.email.value.length == 0) {
					alert("Please enter the e-mail address.");
					return false;
				}
				
				return true;
			}
			
			function doDelete() {
				if (confirm("Are you sure you want to delete this user?")) {
					document.forms[0].action.value = "Delete";
					document.forms[0].submit();
				}
			}
		</script>
	</head>
	<body>
		<h3>CFHibernate User Form</h3>
		
		<p><em>All fields are required.</em></p>
		
		<form action="userForm_process.cfm" method="post" 
			onSubmit="return validate(this);">
		<cfoutput>
			<table border="0" cellpadding="2" cellspacing="1" 
				bgcolor="##999999">
				<tr bgcolor="##ffffff">
					<td align="right"><strong>First Name:</strong></td>
					<td>
						<input type="text" name="firstName" size="50" 
							maxlenth="50" value="#user.getFirstName()#" />
					</td>
				</tr>
				<tr bgcolor="##ffffff">
					<td align="right"><strong>Middle Name:</strong></td>
					<td>
						<input type="text" name="middleName" size="50" 
							maxlenth="50" value="#user.getMiddleName()#" />
					</td>
				</tr>
				<tr bgcolor="##ffffff">
					<td align="right"><strong>Last Name:</strong></td>
					<td>
						<input type="text" name="lastName" size="50" 
							maxlength="50" value="#user.getLastName()#" />
					</td>
				</tr>
				<tr bgcolor="##ffffff">
					<td align="right"><strong>E-Mail:</strong></td>
					<td>
						<input type="text" name="email" size="50" 
							maxlength="50" value="#user.getEmail()#" />
					</td>
				</tr>
				<tr bgcolor="##dedede">
					<td colspan="2" align="center">
					<input type="button" value="Cancel" 
						onClick="javascript:location.href='index.cfm';" />&nbsp;
					<cfif url.action EQ "Update">
						<input type="button" value="Delete User" 
							onClick="javascript:doDelete();" />&nbsp;
					</cfif>
					<input type="submit" value="#url.action# User" />
					</td>
				</tr>
			</table>
			Address Count: #user.getAddresses().size()#<br />
			<cfif user.getAddresses().size()>
				<cfloop from="1" to="#user.getAddresses().size()#" index="i">
					<cfset ad = user.getAddress(JavaCast("int", i))>
					Address #i#: #ad.getAddress1()#, #ad.getCity()#, #ad.getState()# #ad.getZip()#<br />
				</cfloop>
			</cfif>
			<input type="hidden" name="id" value="#user.getId()#" />
			<input type="hidden" name="action" value="#url.action#" />
		</cfoutput>
		</form>
	</body>
</html>

<cfscript>
// cleanup
mySession.close();
sessionFactory.close();
</cfscript>
